<template>
  <div class="jsbos-common-layout">
    <div class="jsbos-common-layout-left">
      <el-tabs v-model="listQuery.category" class="menu-tab">
        <el-tab-pane label="Web菜单" name="Web"></el-tab-pane>
        <el-tab-pane label="App菜单" name="App"></el-tab-pane>
        <div class="jsbos-common-tree-search-box" style="overflow-y: hidden">
          <el-input
            placeholder="输入关键字"
            v-model="filterText"
            style="width: 50%"
            suffix-icon="el-icon-search"
            clearable
          />
          <el-select
            v-model="audit"
            placeholder="请选择"
            style="margin-left: 1%; width: 40%"
          >
            <el-option
              v-for="(item, index) in auditOptions"
              :key="index"
              :label="item.fullName"
              :value="item.id"
            ></el-option>
          </el-select>

          <el-dropdown style="margin-left: 1%">
            <el-link icon="icon-ym icon-ym-mpMenu" :underline="false" />
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="toggleExpand(true)"
                >展开全部</el-dropdown-item
              >
              <el-dropdown-item @click.native="toggleExpand(false)"
                >折叠全部</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-tabs>
      <el-scrollbar
        class="jsbos-common-el-tree-scrollbar"
        v-loading="treeLoading"
      >
        <el-tree
          ref="treeBox"
          :data="treeData"
          :props="defaultProps"
          :default-expand-all="expands"
          highlight-current
          :expand-on-click-node="false"
          node-key="id"
          @node-click="handleNodeClick"
          class="jsbos-common-el-tree"
          v-if="refreshTree"
          :filter-node-method="filterNode"
        >
          <span
            class="custom-tree-node"
            slot-scope="{ data }"
            :title="data.fullName"
          >
            <i :class="data.icon" />
            <span class="text" :title="data.fullName">{{ data.fullName }}</span>
          </span>
        </el-tree>
      </el-scrollbar>
    </div>

    <div class="jsbos-common-layout-center">
      <el-row class="jsbos-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item :label="$t('common.keyword')">
              <el-input
                v-model="userListQuery.keyword"
                placeholder="请输入姓名/账号/手机号"
                clearable
                @keyup.enter.native="search()"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()">
                {{ $t("common.search") }}</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >{{ $t("common.reset") }}
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <div class="jsbos-common-head">
          <div></div>
          <!-- <topOpts @add="addOrUpdateHandle()"></topOpts> -->
          <div class="jsbos-common-head-right">
            <el-tooltip
              effect="dark"
              :content="$t('common.refresh')"
              placement="top"
            >
              <el-link
                icon="icon-ym icon-ym-Refresh jsbos-common-head-icon"
                :underline="false"
                @click="initData()"
              />
            </el-tooltip>
          </div>
        </div>
        <jsbos-table v-loading="listLoading" :data="list">
          <el-table-column prop="code" label="工号" width="100" />
          <el-table-column prop="realName" label="姓名" width="100" />
          <el-table-column prop="account" label="账号" width="100" />
          <el-table-column prop="gender" label="性别" width="90" align="center">
            <template slot-scope="scope" sortable>
              <span>{{
                scope.row.gender == 1
                  ? "男"
                  : scope.row.gender == 2
                  ? "女"
                  : "保密"
              }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="mobilePhone" label="手机" width="120" />
          <el-table-column
            prop="organize"
            label="所属组织"
            min-width="200"
            show-overflow-tooltip
          />
          <el-table-column
            prop="creatorTime"
            label="创建时间"
            :formatter="jsbos.tableDateFormat"
            width="120"
          />
          <el-table-column
            prop="sortCode"
            label="排序"
            width="70"
            align="center"
          />
          <el-table-column
            prop="enabledMark"
            label="状态"
            width="70"
            align="center"
          >
            <template slot-scope="scope">
              <el-tag
                type="success"
                disable-transitions
                v-if="scope.row.enabledMark == 1"
                >正常
              </el-tag>
              <el-tag
                type="warning"
                disable-transitions
                v-else-if="scope.row.enabledMark == 2"
                >锁定
              </el-tag>
              <el-tag type="danger" disable-transitions v-else>禁用</el-tag>
            </template>
          </el-table-column>
        </jsbos-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="getUserLsit"
        />
      </div>
      <Form v-if="formVisible" ref="Form" @refreshDataList="initData" />
      <Diagram
        v-if="diagramVisible"
        ref="Diagram"
        @close="diagramVisible = false"
      />
      <AuthorizeForm
        v-if="authorizeFormVisible"
        ref="AuthorizeForm"
        @close="removeAuthorizeForm"
      />
      <RolePreview
        v-if="rolePreviewVisible"
        ref="RolePreview"
        @close="removeRolePreview"
      />

      <component
        :is="currentView"
        v-if="userRelationListVisible"
        ref="UserRelationList"
        @refreshDataList="initData"
        @closeDialog="userRelationListVisible = false"
      />
    </div>
  </div>
</template>

<script>
import { getDepartmentSelector } from "@/api/permission/department";
import { getRoleList, delRole, updateRoleState } from "@/api/permission/role";
import { getMenuList } from "@/api/system/menu";
import {
  getUserListByMenuId,
  getAllAuthorize,
} from "@/api/permission/userSetting";
export default {
  data() {
    return {
      treeData: [],
      moduleTreeData: [],
      buttonTreeData: [],
      columnTreeData: [],
      resourceTreeData: [],
      formTreeData: [],

      appModuleTreeData: [],
      appButtonTreeData: [],
      appColumnTreeData: [],
      appResourceTreeData: [],
      appFormTreeData: [],
      webModuleTreeData: [],
      webButtonTreeData: [],
      webColumnTreeData: [],
      webResourceTreeData: [],
      webFormTreeData: [],
      defaultProps: {
        children: "children",
        label: "fullName",
      },
      list: [],
      listQuery: {
        keyword: "",
        category: "Web",
        currentPage: 1,
        pageSize: 20,
      },
      userListQuery: {
        keyword: "",
        category: "Web",
        currentPage: 1,
        pageSize: 20,
      },
      auditOptions: [
        {
          fullName: "菜单",
          id: "module",
        },
        {
          fullName: "按钮",
          id: "button",
        },
        {
          fullName: "列表",
          id: "column",
        },
        {
          fullName: "数据",
          id: "resource",
        },
        {
          fullName: "表单",
          id: "form",
        },
      ],
      menuId: "test",
      total: 0,
      listLoading: true,
      treeLoading: false,
      formVisible: false,
      diagramVisible: false,
      authorizeFormVisible: false,
      rolePreviewVisible: false,
      userRelationListVisible: false,
      currentView: null,
      expands: true,
      refreshTree: true,
      filterText: "",
      audit: "module",
    };
  },
  watch: {
    filterText(val) {
      this.$refs.treeBox.filter(val);
    },
    "listQuery.category": function (val) {
      this.initData();
    },
    audit(val) {
      this.chanagType();
    },
  },
  created() {
    this.getMenuList(true);
  },
  methods: {
    //得到左侧树列表
    getMenuList() {
      this.menuId = "test";
      this.filterText = "";
      this.list = [];
      this.initData();
    },
    reset() {
      this.userListQuery.keyword = "";
      this.search();
    },
    search() {
      this.userListQuery = {
        ...this.userListQuery,
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      };
      this.getUserLsit();
    },

    //左侧树折叠/展开
    toggleExpand(expands) {
      this.refreshTree = false;
      this.expands = expands;
      this.$nextTick(() => {
        this.refreshTree = true;
        this.$nextTick(() => {
          this.$refs.treeBox.setCurrentKey(this.companyId);
        });
      });
    },
    //左侧树过滤事件
    filterNode(value, data) {
      if (!value) return true;
      return data.fullName.indexOf(value) !== -1;
    },
    //初始化左侧树
    initData() {
      this.listLoading = true;
      this.treeLoading = true;
      this.moduleTreeData = [];
      this.buttonTreeData = [];
      this.columnTreeData = [];
      this.resourceTreeData = [];
      this.formTreeData = [];
      this.list = [];
      this.appModuleTreeData = [];
      this.appButtonTreeData = [];
      this.appColumnTreeData = [];
      this.appResourceTreeData = [];
      this.appFormTreeData = [];
      this.webModuleTreeData = [];
      this.webButtonTreeData = [];
      this.webColumnTreeData = [];
      this.webResourceTreeData = [];
      this.webFormTreeData = [];
      this.treeData = [];
      getAllAuthorize()
        .then((res) => {
          this.moduleTreeData = res.data.module;
          for (let i = 0; i < this.moduleTreeData.length; i++) {
            console.log(this.moduleTreeData[i].id);
            if (this.moduleTreeData[i].fullName == "App菜单") {
              this.appModuleTreeData.push(this.moduleTreeData[i]);
            } else {
              this.webModuleTreeData.push(this.moduleTreeData[i]);
            }
          }
          this.buttonTreeData = res.data.button;
          for (let i = 0; i < this.buttonTreeData.length; i++) {
            if (this.buttonTreeData[i].fullName == "App菜单") {
              this.appButtonTreeData.push(this.buttonTreeData[i]);
            } else {
              this.webButtonTreeData.push(this.buttonTreeData[i]);
            }
          }
          this.columnTreeData = res.data.column;
          for (let i = 0; i < this.columnTreeData.length; i++) {
            if (this.columnTreeData[i].fullName == "App菜单") {
              this.appColumnTreeData.push(this.columnTreeData[i]);
            } else {
              this.webColumnTreeData.push(this.columnTreeData[i]);
            }
          }
          this.resourceTreeData = res.data.resource;
          for (let i = 0; i < this.resourceTreeData.length; i++) {
            if (this.resourceTreeData[i].fullName == "App菜单") {
              this.appResourceTreeData.push(this.resourceTreeData[i]);
            } else {
              this.webResourceTreeData.push(this.resourceTreeData[i]);
            }
          }
          this.formTreeData = res.data.form;
          for (let i = 0; i < this.formTreeData.length; i++) {
            if (this.formTreeData[i].fullName == "App菜单") {
              this.appFormTreeData.push(this.formTreeData[i]);
            } else {
              this.webFormTreeData.push(this.formTreeData[i]);
            }
          }
          this.$nextTick(() => {
            this.chanagType();
          });

          this.treeLoading = false;
          this.listLoading = false;
        })
        .catch(() => {
          this.treeLoading = false;
          this.listLoading = false;
        });
    },

    chanagType() {
      this.list = [];
      this.treeData = [];
      if (this.audit == "module") {
        if (this.listQuery.category == "Web") {
          this.treeData = this.webModuleTreeData;
        } else {
          this.treeData = this.appModuleTreeData;
        }
      } else if (this.audit == "button") {
        if (this.listQuery.category == "Web") {
          this.treeData = this.webButtonTreeData;
        } else {
          this.treeData = this.appButtonTreeData;
        }
      } else if (this.audit == "column") {
        if (this.listQuery.category == "Web") {
          this.treeData = this.webColumnTreeData;
        } else {
          this.treeData = this.appColumnTreeData;
        }
      } else if (this.audit == "resource") {
        if (this.listQuery.category == "Web") {
          this.treeData = this.webResourceTreeData;
        } else {
          this.treeData = this.appResourceTreeData;
        }
      } else if (this.audit == "form") {
        if (this.listQuery.category == "Web") {
          this.treeData = this.webFormTreeData;
        } else {
          this.treeData = this.appFormTreeData;
        }
      }
      console.log(this.treeData);
    },
    //点击左侧树查询
    handleNodeClick(data) {
      this.menuId = data.id;
      this.getUserLsit();
    },
    //获取人员信息
    getUserLsit() {
      this.listLoading = true;
      getUserListByMenuId(this.menuId, this.userListQuery)
        .then((res) => {
          this.list = res.data.list;
          this.total = res.data.pagination.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
  },
};
</script>
<style scoped>
.jsbos-common-layout .jsbos-common-layout-left .jsbos-common-tree-search-box {
  padding: 0px 0px 0 !important;
}
</style>
